package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
)

/**
美团商店后台
进行修改好的图片数据爬虫
*/

func main() {

	var url string = "https://waimaie.meituan.com/reuse/product/food/r/spuList?opType=0&queryCount=0&pageNum=1&pageSize=30&wmPoiId=9251255&needAllCount=false&needTagList=true" //请求商品地址
	//Cookie
	var token string = "_lxsdk_cuid=175de8402b9c8-0b4ddf2bc6896-4e4c0f20-1fa400-175de8402b9c8; _hc.v=db85c7b7-a57e-825c-1520-e84899fd381e.1605756016; _lxsdk=175de8402b9c8-0b4ddf2bc6896-4e4c0f20-1fa400-175de8402b9c8; uuid=9db3d58ea25c31a4a5ff.1619080209.1.0.0; device_uuid=!a9cb9d7c-7ab3-46e5-ae60-690c0f2a348c; uuid_update=true; __guid=171386534.1560495017446868000.1619080457004.9944; acctId=72586492; token=0vrmHV2gXcu8CAt7tXr6UtObrYNjeiv2eni6zghIf6s0*; brandId=-1; wmPoiId=9251255; isOfflineSelfOpen=0; city_id=440300; isChain=0; existBrandPoi=false; ignore_set_router_proxy=false; region_id=1000440300; region_version=1591171297; newCategory=false; bsid=QDV3HAiQhbq_byAO-MTVeJpWYAWrl9L-n6SEl7Y1_vIpFsp1lWyKFAlubYAajvsiTuIBqZMZP2w3wnMLq2VRbg; logistics_support=1; cityId=440300; provinceId=440000; city_location_id=440300; location_id=440303; pushToken=0vrmHV2gXcu8CAt7tXr6UtObrYNjeiv2eni6zghIf6s0*; wpush_server_url=wss://wpush.meituan.com; shopCategory=food; JSESSIONID=1r9z6a965jk6x1u0ilvukl81y0; monitor_count=1; set_info=%7B%22wmPoiId%22%3A9251255%2C%22region_id%22%3A%221000440300%22%2C%22region_version%22%3A1591171297%7D; logan_custom_report=; _lxsdk_s=17922233f38-f-70e-6b0%7C72586492%7C9; logan_session_token=azhbykugnvqs0mx9atqo" //用户token

	//请求商店商品列表数据
	list := getShopGoodsList(url, token)
	fmt.Println(list)
	//分析返回数据形成数据请求列表
	parseGoodsList(list)
	//遍历列表数据进行图片下载并保存到本地文件

}

/**
分析返回的结构数据，整理成可以请求的url列表
*/
func parseGoodsList(list string) {
	mapData := make(map[string]string, 4)
	json.Unmarshal([]byte(list), &mapData)
	goodData := make(map[string]interface{}, 4)
	json.Unmarshal([]byte(mapData["data"]), &goodData)
	//i := goodData["productList"]

}

/**
设置商品列表请求地址
设置请求的cookie

发起http请求请求到商品列表数据
*/
func getShopGoodsList(url string, token string) (all string) {
	client := &http.Client{}
	request, err := http.NewRequest("GET", url, nil)
	request.Header.Add("Cookie", token)
	result, err := client.Do(request)
	//输出错误
	if err != nil {
		log.Fatal(err)
	}
	defer result.Body.Close()
	goodsResult, _ := ioutil.ReadAll(result.Body)
	log.Printf(string(goodsResult))
	return string(goodsResult)
}
